package com.example.lottery.dao.mapper;

import com.example.lottery.dao.datobject.Encrypt;
import com.example.lottery.dao.datobject.UserDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Insert("insert into user (user_name,email,phone_number,password,identity) values (#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    //    需要获取自增ID → 使用 @Options
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void insert(UserDO userDO);

    @Select("select count(*) from user where phone_number=#{phoneNumber}")
    int countByPhone(@Param("phoneNumber") Encrypt phoneNumber);

    @Select("select count(*) from user where email=#{mail}")
    int countByMail(@Param("mail") String mail);

    @Select("select email from user where phone_number=#{phoneNumber} ")
    String findEmailByPhone(@Param("phoneNumber") Encrypt phoneNumber);


    @Select("select * from user where phone_number=#{phoneNumber}")
    UserDO selectByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);


    @Select("selct * from user where email=#{email}")
    UserDO selectByMail(@Param("email") String email);

    /*
    由于identity可能为null，为null，就查出所有的人员
    故使用动态sql 动态SQL就要依赖<Script>
    使用单引号 不需要转义
     */
    @Select("<script>"+
            " select * from user"+
            " <if test='identity!=null'>"+
            " where identity=#{identity}"+
            " </if>"+
            " order by id desc"+
            " </script>")
    List<UserDO> selectUserListByIdentity(@Param("identity") String identity);

    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    //有foreach或者if之类的 就要使用动态sql标签
    List<Long> selectExistByIds(@Param("items") List<Long> userIds);

    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach item='item' collection='ids' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("ids") List<Long> collect);
}
